package com.luoran.bartalks.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.entity.biz.BtPlanEntry;
import com.luoran.bartalks.service.IBtPlanEntryService;

/**
 * 异常上报Controller
 * 
 * @author wmm
 *
 */
@RestController
@RequestMapping("/api/exception")
public class ExceptionReportApi {

	@Autowired
	private IBtPlanEntryService planEntryService;

	/**
	 * 查询异常信息
	 * 
	 * @return
	 */
	@RequestMapping("/info")
	public JSONObject exceptionInfo(String planEntryId) {
		JSONObject res = new JSONObject();
		BtPlanEntry planEntry = planEntryService.getPlanEntryById(planEntryId);
		if (planEntry == null) {
			res.put("state", false);
			res.put("msg", "系统错误");
			return res;
		}
		res.put("state", true);
		String exceptionInfo = planEntry.getException();
		if (exceptionInfo == null) {
			exceptionInfo = "";
		} else {
			exceptionInfo = exceptionInfo.trim();// 配合angular管道展示
		}
		res.put("exceptionInfo", exceptionInfo);
		res.put("planCode", planEntry.getPlanCode());
		res.put("addressName", planEntry.getAddressName());
		return res;
	}

	/**
	 * 异常信息保存
	 * 
	 * @return
	 */
	@RequestMapping("/report")
	public JSONObject exceptionReport(String planEntryId, String planId, String info) {
		JSONObject res = new JSONObject();
		if (info.length() > 800) {
			res.put("msg", "输入长度限制为800！");
			res.put("state", false);
			return res;
		}
		try {
			planEntryService.updateExceptionInfo(planEntryId, planId, info);
		} catch (Exception e) {
			res.put("msg", "系统错误,请稍后再试");
			res.put("state", false);
			return res;
		}
		res.put("state", true);
		return res;
	}

}
